package Calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public  class Mycalculator extends JFrame implements ActionListener {
    public static String s=null;
    public static String s2=null;
    public static String s3=null;
    public static char symbol='n';
    public static char symbol2='n';

    public static int m=0;
    JPanel container=new JPanel();
    JPanel northP=new JPanel();
    JPanel northP2=new JPanel();
    JTextField jTextField=new JTextField();
//    JTextField jTextField2=new JTextField();
    JButton bt1=new JButton("C");
    JButton bt2=new JButton("<");//退回键
    public Mycalculator() throws HeadlessException {
        this.setTitle("简易计算器");
        this.setSize(400,450);
        this.setLocation(500,300);
        jTextField.setPreferredSize(new Dimension(250,50));
//        jTextField.setPreferredSize(new Dimension(350,50));
        bt1.setPreferredSize(new Dimension(50,50));
        bt2.setPreferredSize(new Dimension(50,50));
        bt1.setFont(new Font("aa",Font.BOLD,20));
        bt1.setForeground(Color.red);
        bt1.addActionListener(new ActionListener() {//添加C清零键
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField.setText("");
                m=0;
                symbol='n';
                symbol2='n';
            }
        });
        bt2.addActionListener(this);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(this.northP2,BorderLayout.NORTH);
        this.add(this.northP,BorderLayout.NORTH);
        this.add(this.container,BorderLayout.CENTER);

        this.northP.add(jTextField);
//        this.northP2.add(jTextField2);
        this.northP.add(bt1);
        this.northP.add(bt2);
        this.container.setLayout(new GridLayout(5,4,9,9));//排版
        this.setResizable(false);//窗口固定
        String button_text="^√%!123+456-789*.0=/";
        for (int i = 0; i < 20; i++) {//创建按钮
            String temp=button_text.substring(i,i+1);
            JButton button=new JButton();
            button.setText(temp);
            this.container.add(button);
            button.addActionListener(this);//事件监听
            button.setFont(new Font("aa",Font.BOLD,25));
        }

        this.jTextField.setFont(new Font("aa",Font.BOLD,25));
        this.jTextField.setEditable(false);
    }

    public static void main(String[] args) {
        Mycalculator mycalculator=new Mycalculator();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String a=e.getActionCommand();
        char arc=a.charAt(0);
        if(arc=='<'){//删除一位数字
            jTextField.setText(s3);
            return;
        }


        if(arc>='0'&&arc<='9'||arc=='.'){
            s3=jTextField.getText();
            jTextField.setText(jTextField.getText()+a);

            if(arc=='.'){
                m=1;
            }
        }
        if((arc=='*'||arc=='-'||arc=='+'||arc=='/'||arc=='%')&&symbol!='n'){// 多项式应用
            s2=jTextField.getText();

            s=""+operation(s,symbol,s2);
            symbol2=arc;
            jTextField.setText("");
        }
        if((arc=='*'||arc=='-'||arc=='+'||arc=='/'||arc=='%')&&symbol2=='n'){
            symbol=arc;
            s=jTextField.getText();
            jTextField.setText("");

        }
        if(arc=='!'||arc=='^'||arc=='√'){//阶乘
            s=jTextField.getText();
            int x=Integer.parseInt(s);
            jTextField.setText(""+jiechen(x,arc));

        }
        if(arc=='='){
//            JTextField
            if(symbol=='n'){
                jTextField.setText(jTextField.getText());
                return;
            }
            s2=jTextField.getText();
            if(symbol2!='n'){
                symbol=symbol2;
            }
            if(m!=1){
                jTextField.setText(""+(int)operation(s,symbol,s2));
            }else if(m==1){
                jTextField.setText(""+operation(s,symbol,s2));
            }
            symbol='n';
            symbol2='n';
        }

    }
        static double operation(String s,char symbol,String s2){
        double a=Double.parseDouble(s);
        double c=Double.parseDouble(s2);
        if(symbol=='+'){
            return a + c;
        }
        if(symbol=='-'){
            return a - c;
        }
        if(symbol=='*'){
            return a * c;
        }
        if (symbol == '/') {
            if(c!=0){
                return a / c;
            }
        }if(symbol=='%'){
            return  a%c;
            }

        return 0;
    }
    static int jiechen(int a,char c){//阶乘平方开根号
        if(c=='!'){
            int sum=1;
            for (int i = a; i >0; i--) {
                sum=sum*a;
            }
            return sum;
        }
        if(c=='^'){

            return a*a;
        }
        if(c=='√'){

            return (int)Math.sqrt(a);
        }
        return 0;

    }
}
